/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.wendy.common.core.utils.reflect;

import java.time.temporal.Temporal;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * 对象类型
 * <p>
 * 这个工具类用于代码分析，对 Java 类进行重新划分，纯粹是按照个人经验做的封装。
 * <p>
 * 对于大部分的项目，可以将 Java 对象，分为下面 8 种类型，每一种数据都有自己的解析方式。
 *
 * <table>
 * <tr><td>VOID         </td><td>函数无返回值</td></tr>
 * <tr><td>COLLECTION   </td><td>集合，包含：set、list，特点：序列化之后是数组。</td></tr>
 * <tr><td>ARRAY        </td><td>数组</td></tr>
 * <tr><td>MAP          </td><td>Map，特点：本身就能替代 JavaBean，但是无法预知有多少字段，无解析价值</td></tr>
 * <tr><td>FIELD        </td><td>字段类型，包含：基础类型，以及：字符串、数字、日期</td></tr>
 * <tr><td>INTERFACE    </td><td>接口</td></tr>
 * <tr><td>ENUM         </td><td>枚举类</td></tr>
 * <tr><td>OBJECT       </td><td>对象模型</td></tr>
 * </table>
 * <p>
 * <p>
 * 后续考虑进一步优化
 *
 * @author Mr.css
 * @version 2021-04-02 10:09
 */
public enum ObjectType {

    VOID(0), COLLECTION(1), ARRAY(2), MAP(3), FIELD(4), INTERFACE(5), ENUM(6), OBJECT(7);

    /**
     * 码值
     */
    private final int value;

    ObjectType(int value) {
        this.value = value;
    }

    /**
     * 返回码值
     *
     * @return 码值
     */
    public int value() {
        return value;
    }


    /**
     * 获取一个对象的类型
     *
     * @param obj 全对象
     * @return ObjectType
     */
    public static ObjectType of(Object obj) {
        return ObjectType.of(obj.getClass());
    }

    /**
     * 根据码值返回类型
     *
     * @param value 码值
     * @return ObjectType
     */
    public static ObjectType valueOf(int value) {
        return ObjectType.values()[value];
    }

    /**
     * 获取一个对象的类型
     *
     * @param clazz 全类名
     * @return ObjectType
     */
    public static ObjectType of(Class<?> clazz) {
        if (void.class == clazz) {
            return VOID;
        } else if (Collection.class.isAssignableFrom(clazz)) {
            return COLLECTION;
        } else if (clazz.isArray()) {
            return ARRAY;
        } else if (Map.class.isAssignableFrom(clazz)) {
            return MAP;
        } else if (isField(clazz)) {
            return FIELD;
        } else if (clazz.isInterface()) {
            return INTERFACE;
        } else if (clazz.isEnum()) {
            return ENUM;
        } else {
            return OBJECT;
        }
    }

    /**
     * 对象是不是MAP
     *
     * @param type 类型
     * @return boolean
     */
    public static boolean isMap(ObjectType type) {
        return type.value == MAP.value;
    }

    /**
     * 对象是不是ARRAY
     *
     * @param type 类型
     * @return boolean
     */
    public static boolean isArray(ObjectType type) {
        return type.value == ARRAY.value;
    }

    /**
     * 对象是不是COLLECTION
     *
     * @param type 类型
     * @return boolean
     */
    public static boolean isCollection(ObjectType type) {
        return type.value == COLLECTION.value;
    }

    /**
     * 对象是不是FIELD
     *
     * @param type 类型
     * @return boolean
     */
    public static boolean isField(ObjectType type) {
        return type.value == FIELD.value;
    }

    /**
     * 对象是不是OBJECT
     *
     * @param type 类型
     * @return boolean
     */
    public static boolean isObject(ObjectType type) {
        return type.value == OBJECT.value;
    }

    /**
     * 对象是不是Void
     *
     * @param type 类型
     * @return boolean
     */
    public static boolean isVoid(ObjectType type) {
        return type.value == VOID.value;
    }

    /**
     * 对象是不是MAP
     *
     * @param clazz 类型
     * @return boolean
     */
    public static boolean isMap(Class<?> clazz) {
        return Map.class.isAssignableFrom(clazz);
    }

    /**
     * 对象是不是ARRAY
     *
     * @param clazz 类型
     * @return boolean
     */
    public static boolean isArray(Class<?> clazz) {
        return clazz.isArray();
    }

    /**
     * 对象是不是COLLECTION
     *
     * @param clazz 类型
     * @return boolean
     */
    public static boolean isCollection(Class<?> clazz) {
        return Collection.class.isAssignableFrom(clazz);
    }

    /**
     * 对象是不是字段
     * <p>
     * 对于代码分析程序来说，这是个很有用的函数，如果对象属于下列之一，将这个对象判定为字段。
     * <p>
     * boolean, char, byte, short, int, long, float, double,
     * String, Number, Date or Temporal。
     *
     * @param clazz Class
     * @return boolean
     */
    public static boolean isField(Class<?> clazz) {
        // Field is boolean, char, byte, short, int, long, float, double.
        return clazz.isPrimitive() || clazz == String.class || Number.class.isAssignableFrom(clazz)
                || Date.class.isAssignableFrom(clazz) || Temporal.class.isAssignableFrom(clazz);
    }

    /**
     * 对象是不是FIELD
     *
     * @param clazz 类型
     * @return boolean
     */
    public static boolean isObject(Class<?> clazz) {
        return ObjectType.of(clazz) == ObjectType.OBJECT;
    }

    /**
     * 对象是不是Void
     *
     * @param clazz 类型
     * @return boolean
     */
    public static boolean isVoid(Class<?> clazz) {
        return Void.class == clazz;
    }


    /**
     * 对象是不是MAP
     *
     * @param obj 独享实例
     * @return boolean
     */
    public static boolean isMap(Object obj) {
        return Map.class.isAssignableFrom(obj.getClass());
    }

    /**
     * 对象是不是ARRAY
     *
     * @param obj 独享实例
     * @return boolean
     */
    public static boolean isArray(Object obj) {
        return obj.getClass().isArray();
    }

    /**
     * 对象是不是COLLECTION
     *
     * @param obj 独享实例
     * @return boolean
     */
    public static boolean isCollection(Object obj) {
        return Collection.class.isAssignableFrom(obj.getClass());
    }

    /**
     * 对象是不是FIELD
     *
     * @param obj 独享实例
     * @return boolean
     */
    public static boolean isField(Object obj) {
        return isField(obj.getClass());
    }

    /**
     * 对象是不是FIELD
     *
     * @param obj 独享实例
     * @return boolean
     */
    public static boolean isObject(Object obj) {
        return ObjectType.of(obj) == ObjectType.OBJECT;
    }

    /**
     * 对象不可能是 void，这个函数可能没意义，暂留
     *
     * @param obj 独享实例
     * @return boolean
     */
    public static boolean isVoid(Object obj) {
        return false;
    }
}
